﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QD.Snapshot.Core.Config;
using QD.Utill;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using QD.Snapshot.Core.BLL;

namespace QD.Code.Sogou
{
    public partial class Form1 : Form
    {
        HttpRequest http = new HttpRequest();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Home();  
        }
        
        int index = 0;
        DateTime startTime = DateTime.MinValue;

        void Search()
        {
            var url = string.Format(Engine.URL_SOGOU, new Random().Next().ToString());
            url = string.Format("https://www.sogou.com/web?query={0}&_asf=www.sogou.com&_ast=1301755099&w=01019900&p=40040100", new Random().Next());
                       
            Echo(index.ToString());
            http = new HttpRequest();
            http.AddCookie("SNUID", textBox2.Text.Trim(), ".sogou.com", DateTime.Now.AddYears(1));
            http.AddCookie("SUV", textBox3.Text.Trim(), ".sogou.com", DateTime.Now.AddMonths(1));



            
            http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
            http.Referer = url;
            var html = http.Requset(url);


            if (html.Contains("验证码"))
            {
                txtMsg.Text = string.Format("验证码：次数：{0},用时：{1}", index, (int)DateTime.Now.Subtract(startTime).TotalSeconds);
            }
            else
                index++;


            if (txtMsg.Text.Length > 2000)
                txtMsg.Text = "";
            txtMsg.Text += "\r\n" + url + "\r\n";
            txtMsg.Text += html.Contains("验证码") ? "验证码\r\n" : "成功";
            txtMsg.Text += "\r\n" + GetCookieHtml(http.m_Cookie);

            if (html.Contains("验证码"))
            {
                textBox2.Text = "";
                txtMsg.Text += html;
            }
            else
            {
                txtMsg.Text += html;
                //Clipboard.SetDataObject(html);
            }
        }      

        void Home()
        {
            var url = "https://www.sogou.com";
            var html = http.Requset(url);
            txtMsg.Text = GetCookieHtml(http.m_Cookie);
        }

        void Check()
        {
            var url = "https://www.sogou.com";
            var html = http.Requset(url);
            txtMsg.Text = GetCookieHtml(http.m_Cookie);
        }

        string GetCookieHtml(CookieContainer cookieContainer)
        {
            var sb=new StringBuilder();
            var cookieList = cookieContainer.GetCookies(new Uri(Engine.URL_SOGOU));
            foreach (Cookie cookie in cookieList)
            {
                sb.AppendFormat("{0} \t  {1}    \t  {2}    \r\n", cookie.Name, cookie.Value, cookie.Expires.ToShortDateString());

                if (cookie.Name == "SNUID")
                    textBox2.Text = cookie.Value;

                if (cookie.Name == "SUID")
                    textBox4.Text = cookie.Value;

                if (cookie.Name == "SUV")
                    textBox3.Text = cookie.Value;
            }
            return sb.ToString();
        }

        Thread thread;
        private void button1_Click(object sender, EventArgs e)
        {
            if (thread != null)
                thread.Abort();
            //index = 0;
            startTime = DateTime.Now;
            //Search();
            thread = new Thread(() =>
            {
                for (int i = 0; i < 1000; i++)
                {
                    Search();
                    Thread.Sleep(100);
                }
            });
            thread.IsBackground = true;
            thread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            index = 0;
            thread.Abort();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Home();
        }

        void Echo(string msg)
        {
            txtMsg.Text = msg;
        }
    }
}
